package com.bookmall.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.bookmall.dao.OrderDao;
import com.bookmall.entity.Order;
import com.bookmall.util.DBUtil;

public class OrderDaoImpl implements OrderDao {

	@Override
	public boolean insert(Order t) throws SQLException {
		// TODO 妃焱燚
		Connection c = DBUtil.getConnection();
		String sql = "insert into order_tb(orderNo,cartId,userId,bookPrice,orderCount,status) values(?,?,?,?,?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, t.getOrderNo());
		ps.setInt(2, t.getCartId());
		ps.setInt(3, t.getUserId());
		ps.setDouble(4, t.getBookPrice());
		ps.setInt(5, t.getOrderCount());
		ps.setInt(6, t.getStatus());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean update(Order t) throws SQLException {
		// TODO 妃焱燚
		Connection c = DBUtil.getConnection();
		String sql = "update order_tb set status = ? where order_id=? and delete_flag=0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, t.getStatus());
		ps.setInt(2, t.getOrderId());
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public boolean delete(int id) throws SQLException {
		// TODO 妃焱燚
		Connection c = DBUtil.getConnection();
		String sql = "update order_tb set delete_flag =1 where order_id=? and delete_flag=0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		int row = ps.executeUpdate();
		DBUtil.closeConnection(c, ps, null);
		return row == 1 ? true : false;
	}

	@Override
	public Order selectById(int id) throws SQLException {
		// TODO 妃焱燚
		Connection c = DBUtil.getConnection();
		String sql = "select orderId,orderNo,cartId,userId,bookPrice,orderCount,status,create_time,modify_time from order_tb where orderId=? and delete_flag=0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			Order o = new Order();
			o.setOrderId(rs.getInt(1));
			o.setOrderNo(rs.getString(2));
			o.setCartId(rs.getInt(3));
			o.setUserId(rs.getInt(4));
			o.setBookPrice(rs.getDouble(5));
			o.setOrderCount(rs.getInt(6));
			o.setStatus(rs.getInt(7));
			o.setCreateTime(rs.getTimestamp(8).toLocalDateTime());
			if (rs.getTimestamp(9) != null) {
				o.setModifyTime(rs.getTimestamp(9).toLocalDateTime());
			}
			// 关闭
			DBUtil.closeConnection(c, ps, rs);
			return o;
		} else {
			// 关闭
			DBUtil.closeConnection(c, ps, rs);
			return null;
		}
	}

	@Override
	public List<Order> selectByPage(int page, int pageSize) throws SQLException {
		// TODO 妃焱燚
		Connection c = DBUtil.getConnection();
		String sql = "select orderId,orderNo,cartId,userId,bookPrice,orderCount,status,create_time,modify_time from order_tb where orderId=? and delete_flag=0 order by create_time desc limit ?,?";
		PreparedStatement ps = c.prepareStatement(sql);
		List<Order> orders = new ArrayList<>();
		ps.setInt(1, (page - 1) * pageSize);
		ps.setInt(2, pageSize);
		ResultSet rs = ps.executeQuery();
		while (rs.next()) {
			Order o = new Order();
			o.setOrderId(rs.getInt(1));
			o.setOrderNo(rs.getString(2));
			o.setCartId(rs.getInt(3));
			o.setUserId(rs.getInt(4));
			o.setBookPrice(rs.getDouble(5));
			o.setOrderCount(rs.getInt(6));
			o.setStatus(rs.getInt(7));
			o.setCreateTime(rs.getTimestamp(8).toLocalDateTime());
			if (rs.getTimestamp(9) != null) {
				o.setModifyTime(rs.getTimestamp(9).toLocalDateTime());
			}
			orders.add(o);
		}
		// 关闭
		DBUtil.closeConnection(c, ps, rs);
		return orders.isEmpty() ? Collections.emptyList() : orders;
	}

	@Override
	public List<Order> selectByIds(List<Integer> ids, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> selectByNo(List<Integer> nos, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(List<Integer> ids) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Order> selectByUserId(int userId, int page, int pageSize) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Order selectByCartId(int cartId) throws SQLException {
		Connection c = DBUtil.getConnection();
		String sql = "select orderId,orderNo,cartId from order_tb left join cart_tb using(cartid) where cartId= ? and cart_tb.delete_flag = 0";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setInt(1, cartId);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			Order o = new Order();
			o.setOrderId(rs.getInt(1));
			o.setOrderNo(rs.getString(2));
			o.setCartId(rs.getInt(3));
			o.setUserId(rs.getInt(4));
			o.setBookPrice(rs.getDouble(5));
			o.setOrderCount(rs.getInt(6));
			o.setStatus(rs.getInt(7));
			o.setCreateTime(rs.getTimestamp(8).toLocalDateTime());
			if (rs.getTimestamp(9) != null) {
				o.setModifyTime(rs.getTimestamp(9).toLocalDateTime());
			}
			// 关闭
			DBUtil.closeConnection(c, ps, rs);
			return o;
		} else {
			// 关闭
			DBUtil.closeConnection(c, ps, rs);
			return null;
		}
	}

	@Override
	public int updateOrderStatus(int orderId, int status) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}
}
